package com.itheima;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.UserDao;
import com.itheima.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;
import java.util.Set;

@SpringBootTest
public class MpWrapper {
    @Autowired
    private UserDao userDao;

    @Test
    void wrapper(){
        /*//第一种
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //大于
        wrapper.gt("age",12);
        //小于
        wrapper.lt("age",18);
        List<User> users = userDao.selectList(wrapper);
        System.out.println(users);*/

        /*//第二种
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age",12)
                .lt("age",18);
        List<User> users = userDao.selectList(wrapper);
        System.out.println(users);*/

        /*//第三种
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().gt(User::getAge,12)
                        .lt(User::getAge,18);
        List<User> users = userDao.selectList(wrapper);
        System.out.println(users);*/

        //第四种  推荐
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.gt(User::getAge,12)
           .lt(User::getAge,18);
        List<User> users = userDao.selectList(lqw);
        System.out.println(users);
    }

    //查询8 或者 12岁的人
    @Test
    void test1(){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.gt(User::getAge,8)
                .or()
                .lt(User::getAge,12);
        List<User> users = userDao.selectList(lqw);
        System.out.println(users);
    }

    //模糊查询 姓张的
    @Test
    void test2(){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.likeRight(User::getName,"张");
        List<User> users = userDao.selectList(lqw);
        System.out.println(users);
    }

    //查询 年龄最小的5个人
    @Test
    void test3(){
        //构建排序
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(User::getAge);

        //构建分页
        IPage<User> page = new Page<>(1,5);

        userDao.selectPage(page,lqw);
        System.out.println(page.getRecords());
    }

    //查询 年龄在8到12之间的人
    @Test
    void test4(){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.between(User::getAge,8,12);
        List<User> users = userDao.selectList(lqw);
        System.out.println(users);
    }

    //查询 年龄最大的5个人
    @Test
    void test5(){
        //构建排序
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(User::getAge);
        //构建分页
        IPage page = new Page(1,5);
        userDao.selectPage(page,lqw);
        System.out.println(page.getRecords());
    }

    //
    @Test
    void test6(){
        Integer ageMax = 18;
        Integer ageMin = 12;
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.select(User::getId,User::getName,User::getAge);
        lqw.ge(ageMin!=null,User::getAge,ageMin);
        lqw.le(ageMax!=null,User::getAge,ageMax);
        List<User> users = userDao.selectList(lqw);
        System.out.println(users);
    }

    //查询 总条数  平均数  总年龄  聚合函数
    @Test
    void test7(){
        QueryWrapper<User> qw = new QueryWrapper<>();

        qw.select("count(*) as count","max(age) as max","avg(age) as avg");

        List<Map<String, Object>> maps = userDao.selectMaps(qw);
        for (Map<String, Object> map : maps) {
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String key = entry.getKey();
                Object value = entry.getValue();
                System.out.println("key="+key+" " + "value="+value);
            }
        }
    }
}
